package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootGj;
import org.footballmanager.pandafootball.system.form.impl.FootGjSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootGjMapper;
import org.footballmanager.pandafootball.system.service.FootGjService;
import org.footballmanager.pandafootball.system.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootGjServiceImpl extends ServiceImpl<FootGj> implements FootGjService {
    @Autowired
    private FootGjMapper footGjMapper;
    @Autowired
    private PhotoService photoService;

    /**
     * 删除国籍记录
     *
     * @param id      主键
     * @param request 请求
     * @return 是否成功
     */
    @Override
    @Transactional
    public int delete(Serializable id, HttpServletRequest request) {
        FootGj t = this.get(id);
        if (t.getLogo() != null) {
            photoService.delete(t.getLogo(), request);
        }
        if (t.getSlt() != null) {
            photoService.delete(t.getSlt(), request);
        }
        log.info("删除国籍:" + t.toString());
        return super.delete(t.getId(), request);
    }

    /**
     * 根据名称查询记录
     *
     * @param name 国籍名称
     * @return 国籍记录
     */
    @Override
    public FootGj findByName(String name) {
        Example example = new Example(FootGj.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("name", name);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 翻页方法
     *
     * @param footGjSearchForm 条件
     * @return 列表
     */
    @Override
    public List<FootGj> findall(FootGjSearchForm footGjSearchForm) {
        return footGjMapper.findall(footGjSearchForm);
    }


    /**
     * 根据主键获取记录
     *
     * @param id 主键
     * @return 记录
     */
    @Override
    public FootGj get(Serializable id) {
        return footGjMapper.findById(id);
    }

    /**
     * 查询
     *
     * @param entityname       实体类名
     * @param map              参数
     * @param footGjSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootGjSearchForm footGjSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footGjSearchForm);
        Page<FootGj> page = PageHelper.startPage(footGjSearchForm.getPageIndex(), footGjSearchForm.getPageSize());
        List<FootGj> list = this.findall(footGjSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footGjSearchForm 条件
     * @param request          请求
     */
    @Override
    public void deleteAll(FootGjSearchForm footGjSearchForm, HttpServletRequest request) {
        if (footGjSearchForm.getSelectIds() != null) {
            String[] result = footGjSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }
}
